export default {
  props: {
    type: { type: String, default: 'image' }, // avatar|video|image 
    maxCount: { type: Number, default: 1 },
    disabled: Boolean 
  },
  methods: {
    chooseMedia() {
      if (this.disabled)  {
        this.$u.toast(' 修改受限，请稍后再试');
        return;
      }
      
      const sourceType = this.type  === 'video' ? ['camera', 'album'] : ['album'];
      
      uni.chooseMedia({ 
        count: this.maxCount, 
        mediaType: this.type  === 'video' ? ['video'] : ['image'],
        sourceType,
        success: (res) => {
          this.uploadFiles(res.tempFiles); 
        }
      });
    },
    async uploadFiles(files) {
      const uploadTasks = files.map(file  => {
        return new Promise((resolve) => {
          uni.uploadFile({ 
            url: 'https://your-api.com/upload', 
            filePath: file.tempFilePath, 
            name: 'file',
            success: (res) => {
              resolve(JSON.parse(res.data).url); 
            }
          });
        });
      });
      
      const urls = await Promise.all(uploadTasks); 
      this.$emit('change', { type: this.type,  urls });
    }
  }
}